
#include "Scene/GameOverScene.h"
#include "ui/CocosGUI.h"
#include "ui/UIButton.h"
#include "Scene/MainMenuScene.h"
#include "AudioEngine.h"
#include <iostream>

USING_NS_CC;
using namespace ui;

Scene* GameOverScene::createScene() {
    return GameOverScene::create();
}

bool GameOverScene::init() {
    if (!Scene::init()) {
        return false;
    }
    AudioEngine::stopAll();
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    auto gameover_delay_lose = DelayTime::create(1.5f); // Delay setting

    // 背景图 Games/GameLeft.png  child 第二个参数设置为 0 
    auto gameover_background_sprite1 = Sprite::create("Games/GameLeft.png");
    gameover_background_sprite1->setPosition(Vec2(visibleSize.width * 0.5, visibleSize.height * 0.5)); // optional: visibleSize.width * 0.5 - 200
    gameover_background_sprite1->setScale(1.0f);
    this->addChild(gameover_background_sprite1, 0);
    auto gameover_background_tint = TintTo::create(2.0f, 125.0f, 125.0f, 125.0f); // background tint, gray (#125.0, #125.0, #125.0)
    auto gameover_background_seq = Sequence::create(gameover_delay_lose, gameover_delay_lose, gameover_background_tint, nullptr);
    gameover_background_sprite1->runAction(gameover_background_seq);

    // Background movement
    //auto background_movement = MoveTo::create(2.0f, Vec2(visibleSize.width * 0.5, visibleSize.height * 0.5));
    ////auto background_seq1 = Sequence::create(background_movement, gameover_delay1, nullptr);
    //gameover_background_sprite1->runAction(background_movement);


    // 失败图片  （Games/gameOver2.png）   child 第二个参数设置为1
    // Create callbacks. Set audio callback to implement the audio delay.
    auto callback_audio = CallFunc::create([]() {
        // 播放音效
        AudioEngine::play2d("Music/losemusic.ogg", false);
        log("Audio Played!");
        });

    // 动作 设置一个延时的动作序列  先延时音效播放的时间 （音乐多久就延长多少秒 ）  然后再把失败的画面 显示出来  使   
    // 动作生成的时间需要调一下
    auto gameover_background_sprite2 = Sprite::create("Games/gameOver2.png");
    gameover_background_sprite2->setPosition(Vec2(visibleSize.width / 2 - visibleSize.width * 0.05,
        visibleSize.height / 2 + visibleSize.height * 0.1));
    gameover_background_sprite2->setScale(1.0f);
    this->addChild(gameover_background_sprite2, 1);


    gameover_background_sprite2->setOpacity(0); // Set the sprite as transparent, otherwise FadeIn wouldn't take effect.
    auto background2_fade_in = FadeIn::create(1.0f); // Fade in
    auto gameover_lose_seq = Sequence::create(/*gameover_delay_lose,*/ callback_audio, /*gameover_delay_lose,*/ background2_fade_in, nullptr); // optional: gameover_background_tint
    gameover_background_sprite2->runAction(gameover_lose_seq);


    // 主菜单  返回主菜单 ui::button
    auto button_return = Button::create("Games/selectSure0.png", "Games/selectSure1.png", "Games/selectSure0.png");
    button_return->setTouchEnabled(true);
    //button_return->setScale(0.1f);
    //button_return->setTitleText("Return"); // button label
    button_return->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type) {
        switch (type)
        {
        case ui::Widget::TouchEventType::BEGAN:
            Director::getInstance()->replaceScene(MainMenuScene::create()); // Background switch, to MainMenuScene
            break;
        case ui::Widget::TouchEventType::ENDED:
            //std::cout << "Button clicked" << std::endl;
            log("Button clicked");
            break;
        default:
            break;
        }
        });
    button_return->setPosition(Vec2(origin.x + visibleSize.width * 0.5 - 20, origin.y + visibleSize.height * 0.18 + 30));
    button_return->setScale(2.0f);
    auto button_return_tint = TintTo::create(2.0f, 255.0f, 255.0f, 0.0f); // button tint, yellow (#255, #255, #0)
    this->addChild(button_return, 2);
    button_return->setOpacity(0);
    auto button_fade_in = FadeIn::create(1.0f); // Button fades in
    auto gameover_seq_button = Sequence::create(gameover_delay_lose, /*gameover_delay_lose,*/ button_fade_in, nullptr);
    button_return->runAction(gameover_seq_button);


    //// 退出按钮  menu
    //MenuItemImage* button_exit2 = MenuItemImage::create("selectReset0.png", "selectReset1.png", this, CC_MENU_SELECTOR(GameOverScene::menuCloseCallback));

    //auto button_exit1 = Label::createWithSystemFont("exit", "Arial", 16);
    //auto name4 = Label::createWithSystemFont("exit", "Arial", 16);
    //auto item2 = MenuItemSprite::create(name3, name4, CC_CALLBACK_1(UserChangeScene::menuCloseCallback, this));
    //item2->setPosition(Vec2::ZERO);
    //auto menu2 = Menu::create(item2, nullptr);
    //menu2->setPosition(visibleSize.width * 0.7, visibleSize.height * 0.3);
    //this->addChild(menu2);

    return true;
}

void GameOverScene::menuCloseCallback(Ref* pSender) {
    //Close the cocos2d-x game scene and quit the application
    Director::getInstance()->end();
}

//void GameOverScene::jumpStartCallback(cocos2d::Ref* pSender) {
//    // 创建主菜单界面并跳转
//    auto scene = MainMenuScene::createScene();
//    Director::getInstance()->replaceScene(scene);
//}

